package app.common.utils.cjt.ao;

import com.qimencloud.api.scene3ldsmu02o9.response.WdtWmsStockoutSalesQuerywithdetailResponse;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 销货单创建
 * @author feizns
 * @since 2023/3/31
 */
@NoArgsConstructor
@Data
public class SaleDeliveryCreateAo {

    /**
     * dto
     */
    private Dto dto;

    /**
     * Dto
     */
    @NoArgsConstructor
    @Data
    public static class Dto {
        /**
         * voucherDate
         */
        private String voucherDate;
        /**
         * externalCode
         */
        private String externalCode;

        /**
         * 代码
         */
        private String code;
        /**
         * customer
         */
        private Customer customer;
        /**
         * businessType
         */
        private BusinessType businessType;
        /**
         * invoiceType
         */
        private InvoiceType invoiceType;
        /**
         * warehouse
         */
        private Warehouse warehouse;
        /**
         * address
         */
        private String address;
        /**
         * linkMan
         */
        private String linkMan;
        /**
         * contactPhone
         */
        private String contactPhone;
        /**
         * memo
         */
        private String memo;
        /**
         * dynamicPropertyKeys
         */
        private List<String> dynamicPropertyKeys;
        /**
         * dynamicPropertyValues
         */
        private List<String> dynamicPropertyValues;
        /**
         * saleDeliveryDetails
         * 这里请求参数过来的时候是对象、序列化时是数组
         */
        private SaleDeliveryDetails saleDeliveryDetailsObj;

        /**
         * 销售发货细节
         */
        private List<SaleDeliveryDetails> saleDeliveryDetails;

        private Project project;

        public List<SaleDeliveryDetails> getSaleDeliveryDetails() {
            return CollectionUtils.isEmpty(saleDeliveryDetails) ? Collections.singletonList(saleDeliveryDetailsObj) : saleDeliveryDetails;
        }

        public static Dto from(WdtWmsStockoutSalesQuerywithdetailResponse.Order order) {
            Dto dto = new Dto();
            dto.setBusinessType(new BusinessType("15"));
            dto.setExternalCode(order.getOrderNo());
            dto.setProject(new Project(afterTruncationBar(order.getShopNo())));
            dto.setCustomer(new Customer(order.getShopNo()));
            dto.setCode(order.getOrderNo());
            dto.setVoucherDate(order.getConsignTime());
            dto.setMemo(String.format("(淘系)-编号：[%s] 备注：[%s] 类型：[%s] 订单编号：[%s] 系统订单编号：[%s]", dto.getExternalCode(), order.getRemark(), "销售出库单", order.getTradeNo(), order.getSrcTradeNo()));
            dto.setWarehouse(new Warehouse(order.getWarehouseNo()));
            List<SaleDeliveryDetails> list = order.getDetailsList().stream()
                    .map(SaleDeliveryDetails::from)
                    .collect(Collectors.toList());
            dto.setSaleDeliveryDetails(list);
            return dto;
        }

        /**
         * 将原字符中的'-'截断：例："023-1" -> "023"
         * @param shopNo 商店没有
         * @return {@link String}
         */
        public static String afterTruncationBar(String shopNo) {
            if ( shopNo != null ) {
                int index = shopNo.indexOf("-");
                if ( index != -1 )
                    return shopNo.substring(0, index);
            }
            return shopNo;
        }

        /**
         * Customer
         */
        @NoArgsConstructor
        @Data
        @AllArgsConstructor
        public static class Customer {
            /**
             * code
             */
            private String code;
        }
        /**
         * Project
         */
        @NoArgsConstructor
        @Data
        public static class Project {
            /**
             * code
             */
            private String code;

            public Project(String code) {
                this.code = afterTruncationBar(code);
            }
        }

        /**
         * BusinessType
         */
        @NoArgsConstructor
        @Data
        @AllArgsConstructor
        public static class BusinessType {
            /**
             * code
             */
            private String code;
        }

        /**
         * InvoiceType
         */
        @NoArgsConstructor
        @Data
        public static class InvoiceType {
            /**
             * code
             */
            private String code;
        }

        /**
         * Warehouse
         */
        @NoArgsConstructor
        @AllArgsConstructor
        @Data
        public static class Warehouse {
            /**
             * code
             */
            private String code;
        }

        /**
         * SaleDeliveryDetails
         */
        @NoArgsConstructor
        @Data
        public static class SaleDeliveryDetails {

            /**
             * inventory
             */
            private Inventory inventory;
            /**
             * unit
             */
            private Unit unit;

            /**
             * 税率
             */
            private BigDecimal taxRate;
            /**
             * quantity
             */
            private Integer quantity;
            /**
             * origTaxAmount
             */
            private BigDecimal origTaxAmount;

            /**
             * 源自折扣价格
             */
            private BigDecimal OrigDiscountPrice;
            /**
             * 源自折扣价格
             */
            private BigDecimal OrigDiscountAmount;

            /**
             * 源自税价格
             */
            private BigDecimal origTaxPrice;
            /**
             * dynamicPropertyKeys
             */
            private List<String> dynamicPropertyKeys;
            /**
             * dynamicPropertyValues
             */
            private List<String> dynamicPropertyValues;

            public static SaleDeliveryDetails from(WdtWmsStockoutSalesQuerywithdetailResponse.DetailsList detailsList) {
                SaleDeliveryDetails details = new SaleDeliveryDetails();
                details.setInventory(new Inventory(detailsList.getSpecNo()));
                details.setQuantity(Double.valueOf(detailsList.getGoodsCount()).intValue());
                details.setOrigDiscountPrice(new BigDecimal(detailsList.getMarketPrice()));
                if ( detailsList.getTaxRate() != null )
                    details.setTaxRate(new BigDecimal(detailsList.getTaxRate()));
                else
                    details.setTaxRate(BigDecimal.ZERO);
                details.setOrigDiscountAmount(new BigDecimal(detailsList.getShareAmount()));
                details.setOrigTaxPrice(details.getOrigDiscountPrice());
                details.setOrigTaxAmount(details.getOrigDiscountAmount());
                details.setUnit(new Unit(detailsList.getUnitName()));
                return details;
            }

            /**
             * Inventory
             */
            @NoArgsConstructor
            @Data
            @AllArgsConstructor
            public static class Inventory {
                /**
                 * code
                 */
                private String code;
            }

            /**
             * Unit
             */
            @NoArgsConstructor
            @Data
            @AllArgsConstructor
            public static class Unit {
                /**
                 * name
                 */
                private String name;
            }
        }
    }
}
